<template>
  <a-modal
    :title="title"
    :width="580"
    :visible="visible"
    :isLoading="isLoading"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="isLoading">
      <a-alert class="mb-30" :showIcon="true" message="功能说明" banner>
        <template slot="description">
          <p>1. 用于打印发货物流的电子面单，无需人工录入单号，提升效率；</p>
          <p>2. 获取电子面单成功后，将自动更新订单状态为已发货；</p>
        </template>
      </a-alert>
      <a-form :form="form">
        <a-form-item label="电子面单模板" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            v-decorator="['templateId', { rules: [{ required: true, message: '请选择电子面单模板' }] }]"
            placeholder="请选择电子面单模板"
          >
            <a-select-option
              v-for="(item, index) in templateList"
              :key="index"
              :value="item.template_id"
            >{{ item.name }}</a-select-option>
          </a-select>
          <div class="form-item-help">
            <router-link :to="{ path: '/apps/eorder/template/index' }">模板管理</router-link>
          </div>
        </a-form-item>
        <a-form-item label="发货地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            v-decorator="['addressId', { rules: [{ required: true, message: '请选择发货地址' }] }]"
            placeholder="请选择发货地址"
          >
            <a-select-option
              v-for="(item, index) in addressList"
              :key="index"
              :value="item.address_id"
            >{{ item.full_address }}</a-select-option>
          </a-select>
          <div class="form-item-help">
            <router-link :to="{ path: '/store/address/index' }">地址管理</router-link>
          </div>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import * as OrderApi from '@/api/order/event'
import * as TemplateApi from '@/api/eorder/template'
import * as AddressApi from '@/api/store/address'
import { AddressTypeEnum } from '@/common/enum/store/address'
import { getLodop } from './lodopFuncs'

export default {
  data () {
    return {
      // 对话框标题
      title: '打印电子面单',
      // 标签布局属性
      labelCol: { span: 7 },
      // 输入框布局属性
      wrapperCol: { span: 13 },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      isLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 商家发货地址列表
      addressList: [],
      // 电子面单模板列表
      templateList: [],
      // 当前记录
      record: {},
      // 打印控件CLodop
      LODOP: undefined
    }
  },
  async created () {
    this.isLoading = true
    // 获取商家发货地址
    await this.getAddressList()
    // 电子面单模板列表
    await this.getTemplateList()
    this.isLoading = false
  },
  methods: {

    // 显示对话框
    handle (record) {
      if (getLodop()) {
        // 显示窗口
        this.visible = true
        // 当前记录
        this.record = record
      }
    },

    // 获取商家发货地址
    async getAddressList () {
      await AddressApi.all({ type: AddressTypeEnum.DELIVERY.value })
        .then(result => this.addressList = result.data.list)
    },

    // 获取电子面单模板列表
    async getTemplateList () {
      await TemplateApi.all()
        .then(result => this.templateList = result.data.list)
    },

    // 确认按钮
    handleSubmit (e) {
      e.preventDefault()
      // 表单验证
      const { form: { validateFields } } = this
      validateFields((errors, values) => {
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    // 关闭对话框事件
    handleCancel () {
      this.visible = false
      this.form.resetFields()
    },

    // 提交到后端api
    onFormSubmit (values) {
      this.isLoading = true
      OrderApi.eorder({ orderId: this.record.order_id, form: values })
        .then(result => {
          // 显示成功
          this.$message.success(result.message, 1.5)
          // 关闭对话框事件
          this.handleCancel()
          // 执行打印
          this.printEOrder(result.data.template)
          // 通知父端组件提交完成了
          this.$emit('handleSubmit', values)
        })
        .finally(() => this.isLoading = false)
    },

    // 调用打印控件
    printEOrder (template) {
      LODOP.PRINT_INIT()
      LODOP.SET_PRINT_PAGESIZE(0)

      LODOP.NewPageA()
      LODOP.ADD_PRINT_HTML(0, 0, "100%", "100%", template)

      // LODOP.NewPageA()
      // LODOP.ADD_PRINT_HTML(0, 0, "100%", "100%", template)

      LODOP.SET_PRINT_MODE("AUTO_CLOSE_PREWINDOW", true)
      LODOP.SET_PRINT_MODE("CATCH_PRINT_STATUS", true)
      LODOP.SET_PRINT_MODE("PRINT_PAGE_PERCENT", "Auto-Width")
      LODOP.SET_SHOW_MODE("HIDE_PAGE_PERCENT", true)
      LODOP.PREVIEW()
    }

  }
}
</script>
